local ByteArray = require("framework.cc.utils.ByteArray");
local Packet = class("Packet",ByteArray)

--协议格式  长度 + 协议号 + 数据

--  uInt32  数据长度  4字节
--  uInt16  协议ID    2字节
--  数据

--[[
    local ByteArray = require("framework.cc.utils.ByteArray");
    local ba = nil;
    local bb = nil;
    local old = false   
    if old then
        -- body
        ba = ByteArray.new();
        ba:setPos(1);
        ba:writeByte(0x59)
        ba:writeInt(11)
        ba:writeShort(1101)
        ba:writeStringUShort("")
        ba:writeStringUShort("中文")
    else
        ba = Packet.new();
        ba:setPos(5);
        ba:pWriteUint8(0x59);
        ba:pWriteInt32(11);
        ba:pWriteInt16(1101);
        ba:writeStringUShort("")
        ba:writeStringUShort("中文")
    end
	print(ba:toString());
    print("11111111111111111111111111111111");
    if old then
        -- body
        bb = ByteArray.new();
        bb:writeBytes(ba);
        bb:setPos(1);
        print(bb:readByte());
        print(bb:readInt());
        print(bb:readShort());
        print(bb:readStringUShort());
        print(bb:readStringUShort());
    else
        print("xxxxxxxxxxxxxxxxxx");
        bb = Packet.new();
        bb:writeBytes(ba);
        bb:setPos(5);
        print(bb:pReadUint8());
        print(bb:pReadInt32());
        print(bb:pReadInt16());
        print(bb:readStringUShort());
        print(bb:readStringUShort());
    end
]]


function Packet:ctor(__endian,__pos)
    -- body
    __endian = __endian or ByteArray.ENDIAN_BIG;
    __pos = __pos or 1;
    Packet.super.ctor(self,__endian);
    self.length = 0;
    self:setPos(__pos);
end

------------------------------------------------------------------Write-------------------------------------------------------------------
function Packet:addDataLength(l)
    -- body
    self.length = self.length + l;
end


function Packet:pWriteUint8(data)
    -- body
    local oldPos = self:getPos();
    self:writeByte(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteUint16(data)
    -- body
    local oldPos = self:getPos();
    self:writeUShort(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteInt16(data)
    -- body
    local oldPos = self:getPos();
    self:writeShort(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteUint32(data)
    -- body
    local oldPos = self:getPos();
    self:writeUInt(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteInt32(data)
    -- body
    local oldPos = self:getPos();
    self:writeInt(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end


function Packet:pWriteFloat(data)
    -- body
    local oldPos = self:getPos();
    self:writeFloat(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteDouble(data)
    -- body
    local oldPos = self:getPos();
    self:writeDouble(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteString(data)
    -- body
    local dataLength = string.len( data );
    self:pWriteUint32(dataLength);
    oldPos = self:getPos();
    self:writeString(data);
    newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end


function Packet:pWriteCharArray(data)
    -- body
    local oldPos = self:getPos();
    self:writeStringBytes(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteBool(data)
    -- body
    local oldPos = self:getPos();
    self:writeBool(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteBuf(data)
    -- body
    self:writeBuf(data);
end

function Packet:pWriteEnd()
    -- body
    self:setPos(1);
    self:pWriteUint32(self.length);
    mgrs.log:printDebug("sendData:" .. self:toString());
end


---------------------------------------------------------------------Read-------------------------------------------------------------
function Packet:pReadLength()
    -- body
    return self:pReadUint32();
end

function Packet:pReadProtoId()
    -- body
    return self:pReadUint16();
end

function Packet:pReadLast()
    -- body
    
end


function Packet:pReadUint8()
    -- body
    return self:readByte();
end

function Packet:pReadUint16()
    -- body
    return self:readUShort();
end

function Packet:pReadInt16()
    -- body
    return self:readShort();
end

function Packet:pReadUint32()
    -- body
    return self:readUInt();
end

function Packet:pReadInt32()
    -- body
    return self:readInt();
end

function Packet:pReadFloat()
    -- body
    return self:readFloat();
end

function Packet:pReadDouble()
    -- body
    return self:readDouble();
end

function Packet:pReadString()
    -- body
    local length = self:pReadUint32();
    return self:readStringBytes(length);
end

function Packet:pReadCharArray()
    -- body
    return {};
end

function Packet:pReadBool()
    -- body
    return self:readBool();
end

function Packet:pReadBuf()
    -- body
    return {};
end



return Packet;
